'use strict';

angular.module('com.lx.ngsl.role.controller', [])
	/**
	 * 列表页面controller
	 */
  .controller('roleListController', ['$scope', '$http', '$location', '$alert', 'roleService', function($scope, $http, $location, $alert, roleService) {
	$scope.items = [];
	$scope.maxSize = 5;
	$scope.pagesize = 5;
	$scope.totalItems = roleService.totalItems;
	$scope.currentPage = roleService.currentPage;

	$scope.pageChanged = function() {
		$scope.query($scope.currentPage);
	};
	$scope.searchItem = roleService.searchItem;
	$scope.selectedroleId = roleService.selectedroleId;
	
	//查询
	$scope.query = function(page) {
		page = page ? (page-1) : 0;
		$http({
			method:"GET",
			url:"/ngsl/v0/roles",
			params:{
				"page":page,
				"size":$scope.pagesize,
				"sort":"name,asc",
				"name":$scope.searchItem.name,
				"description":$scope.searchItem.description
			}
		}).success(
				function(data, status) {
					$scope.items = data.content;
					$scope.totalItems = data.totalElements;
				}).error(function(data, status) {
  					alert(data.error);
					$scope.data = data || "Request failed";
					$scope.status = status;
		});
	};
	
	//新增
	$scope.add = function() {
		$location.path('/role/add');
	}
	
	//删除
	$scope.del = function(item) {
		var id = item.id;
		$http({
			method : 'delete',
			url : '/ngsl/v0/role/'+id,
		}).success(function(data, status) {
			$alert.tip("操作成功!");
			$scope.query($scope.currentPage);
		}).error(function(data, status) {
			alert(data.error);
			$scope.data = data || "Request failed";
			$scope.status = status;
		});
	}

	
	$scope.clear = function(){
		$scope.searchItem = {
				name:"",
				description:""
		};
	}
	
	$scope.query($scope.currentPage);
	
  }])
	/**
	 * 新增页面controller
	 */
  .controller('roleAddController', ['$scope', '$http', '$location', '$alert', 'roleService', function($scope, $http, $location, $alert, roleService) {
	  	$scope.submitted = false;
	  	$scope.addItem = {};
	  
	    
	    
		$scope.submitted = false;
		// 新增保存
		$scope.save = function() {
			if ($scope.addForm.$valid) {
				$http({
					method : 'post',
					url : '/ngsl/v0/role',
					data : $scope.addItem,
				}).success(
						function(data, status) {
							if(data == false){
								$alert.error({message:"该角色名已存在！"});
							}else{
								$alert.tip("操作成功!");
								$scope.backToList();
							}
						}).error(function(data, status) {
		  					alert(data.error);
							$scope.data = data || "Request failed";
							$scope.status = status;
				});
			} else {
				$scope.submitted = true;
			}
		}
		
		$scope.backToList = function() {
			$location.path('/role/list');
		};
		
  }])
 
  	







